#include "filename.h"

#ifdef _WIN32 // windows
#include <io.h>
#ifdef _WIN64

// std::vector<std::string> get_filenames(std::string path) {
//     std::vector<std::string> filenames;
//     _finddata_t fileinfo;
//     intptr_t hFile = 0;
//     std::string p;
//     if ((hFile = _findfirst(p.assign(path).append("\\").c_str(), &fileinfo)) != -1) {
//         do {
//             if ((fileinfo.attrib & _A_SUBDIR)) {
//                 continue;
//             } else {
//                 filenames.push_back(p.assign(path).append("\\").append(fileinfo.name));
//             }
//         } while (_findnext(hFile, &fileinfo) == 0);

//         _findclose(hFile);
//     }
//     return filenames;
// }
#include <cstring>
#include <io.h>

std::vector<std::string> get_filenames(std::string path) {
    intptr_t FileHandle;
    struct _finddata_t FileInfo;

    std::vector<std::string> files;

    if ((FileHandle = _findfirst(path.append("\\*").c_str(), &FileInfo)) == -1) {
        throw std::string("path not exist");
    } else {
        while (_findnext(FileHandle, &FileInfo) == 0) {
            if (strcmp(FileInfo.name, ".") && strcmp(FileInfo.name, "..")) {
            files.push_back(FileInfo.name);
            }
        }
    }

    _findclose(FileHandle);
    return files;
}

#else

#include <cstring>
#include <io.h>

std::vector<std::string> get_filenames(std::string path) {
    intptr_t FileHandle;
    struct _finddata_t FileInfo;

    std::vector<std::string> files;

    if ((FileHandle = _findfirst(path.append("\\*").c_str(), &FileInfo)) == -1) {
        throw std::string("path not exist");
    } else {
        while (_findnext(FileHandle, &FileInfo) == 0) {
            if (strcmp(FileInfo.name, ".") && strcmp(FileInfo.name, "..")) {
            files.push_back(FileInfo.name);
            }
        }
    }

    _findclose(FileHandle);
    return files;
}

#endif
#endif

#ifdef __linux__ // linux
#include <cstdio>
#include <dirent.h>

std::vector<std::string> get_filenames(std::string path) {
    DIR *d = opendir(path.c_str());
    if (d == NULL) {
        throw std::string("path not exist");
    }
    struct dirent *entry;
    std::vector<std::string> filenames;
    while ((entry = readdir(d)) != NULL) {
        std::string filename(entry->d_name);
        if (filename.compare(".") && filename.compare(".."))
            filenames.push_back(filename);
    }
    closedir(d);
    return filenames;
}

#endif

#ifdef __ANDROID__ // android

std::vector<std::string> get_filenames(std::string path) {}

#endif
